package com.longtop.framework.sys.func.web;

import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.sys.func.model.TSysFunc;
import com.longtop.framework.sys.func.service.FuncService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 系统管理-功能
 * 
 * @author SJH
 * 
 */
public class FuncListAction extends BaseAction {

	private FuncService service;

	private final String DATASET_FUNC = "Dataset_Func";

	private final String DATASET_MENU = "Dataset_Menu";

	public FuncListAction() {
		this.service = this.getBean(FuncService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		Dataset dataset = this.getDataset(context, DATASET_MENU);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_MENU)) {
			return;
		}
		List menuList = service.getMenuList();
		BeanUtil.listToDataset(menuList, dataset, false);
	}

	/**
	 * 提交保存
	 * 
	 * @param context
	 */
	public void saveFunc(IActionContext context) {
		Dataset funcDataset = this.getDataset(context, DATASET_FUNC);
		if (!WebUtil.validateDatasetChange(this, funcDataset, DATASET_FUNC)) {
			return;
		}
		try {
			Map changeMap = WebUtil.datasetToObjectList(funcDataset, "funcId", TSysFunc.class);
			Integer[] count = service.saveFunc(changeMap);
			String msg = WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue());
			this.hintInfoMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("保存功能失败。");
		}
	}

	/**
	 * 加载数据
	 * 
	 * @param context
	 */
	public void loadFunc(IActionContext context) {
		Dataset funcDataset = this.getDataset(context, DATASET_FUNC);
		if (!WebUtil.validateDatasetExist(this, funcDataset, DATASET_FUNC)) {
			return;
		}
		// 获得页面菜单ID
		String menuId = context.getCommandParameterValue("menuId");

		QueryConditions queryConditions = WebUtil.createQueryConditions();
		queryConditions.setCondition("menuId", QueryConditions.EQUAL, menuId);

		QueryResult queryResult = service.loadFunc(queryConditions);

		try {
			BeanUtil.queryResultToDataset(queryResult, funcDataset, false, true);
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("功能读取出错。");
		}
	}
}